<?php

namespace App\Http\Controllers;

use App\Wish;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Rules\Sensitiveword;

class WishController extends Controller
{
    public function create()
    {
        $validator = Validator::make(\request()->toArray(), [
           'content' => [
               'required',
               'min:1',
               'max:45',
               new Sensitiveword(),
           ],

        ], [
            'content.required' => '请填写完再提交',
            'content.min' => '字数至少一个',
            'content.max' => '字数不能超过45个'
        ]);
        if ($validator->fails()) {
            return $validator->errors();
        }

        $wish = new Wish();
        $wish->fill(\request()->all());
        $wish->save();
        return response()->json(['id' => $wish->id])->setStatusCode(201);
    }

    public function wishing()
    {
        Validator::make(\request()->toArray(), [
            'id' => 'required'
        ], [
            'id.required' => '参数错误',
        ])->validate();
        $wish = Wish::find(\request('id'));
        $wish->status = 1;
        $wish->save();
        return response()->json(['status' => true]);
    }

    public function wishes()
    {
        return response()->json(Wish::all(['id', 'content', 'status', 'created_at']));
    }
}
